<template>
  <div class="mod-attachment__attachment">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.fileName" placeholder="原始文件名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('attachment:attachment:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
<!--      <el-form-item>-->
<!--        <el-button v-if="state.hasPermission('attachment:attachment:delete')" type="danger" @click="state.deleteHandle()">删除</el-button>-->
<!--      </el-form-item>-->
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
              <el-table-column prop="businessId" label="业务ID" header-align="center" align="center"></el-table-column>
              <el-table-column prop="tableName" label="业务表名" header-align="center" align="center"></el-table-column>
              <el-table-column prop="fileName" label="文件名称" header-align="center" align="center"></el-table-column>
<!--              <el-table-column prop="fileSaveName" label="附件存储名称" header-align="center" align="center"></el-table-column>-->
              <el-table-column prop="fileSuffix" label="文件类型" header-align="center" align="center"></el-table-column>
              <el-table-column prop="fileSize" label="附件的大小" header-align="center" align="center"></el-table-column>
              <el-table-column prop="url" label="附件存储地址" header-align="center" align="center" width="250"></el-table-column>
              <el-table-column prop="type" label="附件的类型" header-align="center" align="center">
                <template v-slot="scope">
                  {{ state.getDictLabel("attment_type", scope.row.type) }}
                </template>
              </el-table-column>
              <el-table-column prop="status" label="附件的状态" header-align="center" align="center">
                <template v-slot="scope">
                  {{ state.getDictLabel("attment_status", scope.row.status) }}
                </template>
              </el-table-column>
              <el-table-column prop="remark" label="备注说明" header-align="center" align="center"></el-table-column>
<!--              <el-table-column prop="createUser" label="项目创建人" header-align="center" align="center"></el-table-column>-->
<!--              <el-table-column prop="createTime" label="项目创建时间，格式：YYYY-MM-DD HH:MM:SS" header-align="center" align="center"></el-table-column>-->
<!--              <el-table-column prop="updateUser" label="最后更新人" header-align="center" align="center"></el-table-column>-->
<!--              <el-table-column prop="updateTime" label="最后更新时间，格式：YYYY-MM-DD HH:MM:SS" header-align="center" align="center"></el-table-column>-->
            <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
        <template v-slot="scope">
          <el-button type="primary" link @click="detailHandle(scope.row.attachmentId)">详情</el-button>
          <el-button v-if="state.hasPermission('attachment:attachment:update')" type="primary" link @click="addOrUpdateHandle(scope.row.attachmentId)">修改</el-button>
          <el-button v-if="state.hasPermission('attachment:attachment:delete')" type="primary" link @click="state.deleteHandle(scope.row.attachmentId)">删除</el-button>
          <el-button type="primary" v-if="scope.row.status != 0" link @click="doDownload(scope.row.url, scope.row.fileName)">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList">确定</add-or-update>
    <detail ref="detailRef" @refreshDataList="state.getDataList">确定</detail>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs } from "vue";
import AddOrUpdate from "./attachment-add-or-update.vue";
import detail from "./attachment-detail.vue";

const view = reactive({
  deleteIsBatch: true,
  getDataListURL: "/attachment/attachment/page",
  getDataListIsPage: true,
  exportURL: "/attachment/attachment/export",
  deleteURL: "/attachment/attachment"
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};

const detailRef = ref();
const detailHandle = (id?: number) => {
  detailRef.value.init(id);
};

const doDownload = (url, fileName) => {
      // this.getBlob(url).then(res =>{ //url:文件在oss上的地址
      //     this.saveAs(res,fileName) // filename:文件名，可自定义
      // })
      console.log( fileName);
      let link = document.createElement("a");
      link.href = url;
      link.setAttribute("download", fileName);
      link.click();
      link.remove();
    }

</script>
